import 'package:flutter/material.dart';
import 'package:yeyu_net/core/hi_error.dart';
import 'package:flutter_bilibili/http/dao/profile_dao.dart';
import 'package:flutter_bilibili/model/profile_model.dart';
import 'package:flutter_bilibili/util/toast.dart';
import 'package:flutter_bilibili/util/view_util.dart';
import 'package:flutter_bilibili/widget/benefit_card.dart';
import 'package:flutter_bilibili/widget/dark_mode_item.dart';
import 'package:flutter_bilibili/widget/hi_banner.dart';
import 'package:flutter_bilibili/widget/hi_blur.dart';
import 'package:flutter_bilibili/widget/hi_flexible_header.dart';
import 'package:flutter_bilibili/widget/course_card.dart';

class ProfilePage extends StatefulWidget {
  @override
  _ProfilePageState createState() => _ProfilePageState();
}

class _ProfilePageState extends State<ProfilePage> with AutomaticKeepAliveClientMixin{
  ProfileModel _profileModel;
  ScrollController _controller = ScrollController();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _loadData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: NestedScrollView(
        controller: _controller,
        headerSliverBuilder: (BuildContext context, bool bodyIsScrolled) {
          return <Widget>[_buildAppBar()];
        },
        body: ListView(
          padding: EdgeInsets.only(top:10),
          children: [...buildContentList()],
        ),
      ),
    );
  }

  void _loadData() async {
    try {
      ProfileModel result = await ProfileDao.get();
      setState(() {
        _profileModel = result;
      });
    } on NeedAuth catch (e) {
      print(e);
      showWarnToast(e.message);
    } on HiNetError catch (e) {
      print(e);
      showWarnToast(e.message);
    }
  }

  _buildHead() {
    if (_profileModel == null) return Container();
    return HiFlexibleHeader(
        controller: _controller,
        name: _profileModel.name,
        face: _profileModel.face);
  }

  _buildAppBar() {
    return SliverAppBar(
      expandedHeight: 160,
      pinned: true,
      flexibleSpace: FlexibleSpaceBar(
        titlePadding: EdgeInsets.only(left: 0),
        title: _buildHead(),
        background: Stack(
          children: [
            Positioned.fill(
                child: cachedImage(
                    'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.win4000.com%2Fwallpaper%2F9%2F598a7e6eda231.jpg&refer=http%3A%2F%2Fpic1.win4000.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1624609752&t=d05c5a13d9107d36fa849cb1a12848a3')),
            Positioned.fill(child: HiBlur(sigma: 1)),
            Positioned(child: _buildProfileTab(), bottom: 0, left: 0, right: 0)
          ],
        ),
      ),
    );
  }

  buildContentList() {
    if (_profileModel == null) {
      return [];
    }
    return [_buildBanner(),CourseCard(courseList: _profileModel.courseList),BenefitCard(benefitList: _profileModel.benefitList),DarkModeItem()];
  }

  _buildBanner() {
    return HiBanner(_profileModel.bannerList,
        bannerHeight: 120, padding: EdgeInsets.only(left: 10, right: 10));
  }

  _buildProfileTab() {
    if (_profileModel == null) return Container();
    return Container(
      padding: EdgeInsets.only(top:5,bottom:5),
      decoration: BoxDecoration(
        color: Colors.white54
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          _buildIconText('收藏', _profileModel.favorite),
          _buildIconText('点赞', _profileModel.like),
          _buildIconText('浏览', _profileModel.browsing),
          _buildIconText('金币', _profileModel.coin),
          _buildIconText('粉丝', _profileModel.fans)
        ],
      ),
    );
  }

  _buildIconText(String text, int count) {
    return Column(
      children: [
        Text('$count', style: TextStyle(fontSize: 15, color: Colors.black87)),
        Text(text, style: TextStyle(fontSize: 12, color: Colors.grey[600]))
      ],
    );
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
